/*
 * Copyright 2011 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.gaellalire.jzip.view;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

/**
 *
 * @author Gael Lalire
 */
public class JZipFrame extends JFrame {

    private static final long serialVersionUID = 4256212673940966965L;

    private JZipPanel zipPanel;

    public JZipFrame(final File file) {
        this(file, null);
    }

    public JZipFrame(final File file, final ActionListener openActionListener) {
        super("JZip");
        zipPanel = new JZipPanel(file);
        getContentPane().add(zipPanel);
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Fichier");
        JMenuItem item;
        if (openActionListener != null) {
            item = new JMenuItem("Ouvrir...");
            item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            item.addActionListener(openActionListener);
            menu.add(item);
        }

        item = new JMenuItem("Fermer");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                dispose();
            }
        });
        menu.add(item);
        menuBar.add(menu);

        menu = new JMenu("Édition");
        item = new JMenuItem("Tout sélectionner");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                zipPanel.selectAll();
            }
        });
        menu.add(item);
        menuBar.add(menu);

        menu = new JMenu("Aller");
        item = new JMenuItem("Dossier parent");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_UP, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                zipPanel.goParent();
            }
        });
        menu.add(item);

        item = new JMenuItem("Explorer / Lancer");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                zipPanel.goExplore();
            }
        });
        menu.add(item);

        menuBar.add(menu);


        menu = new JMenu("Fenêtre");
        item = new JMenuItem("Activité");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.ALT_DOWN_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                JZipActivitiesFrame.toggleVisibility();
            }
        });
        menu.add(item);
        menuBar.add(menu);


        setJMenuBar(menuBar);
        pack();
        zipPanel.focusTable();
    }

    @Override
    public void dispose() {
        zipPanel.dispose();
        super.dispose();
    }

}
